# -*- coding: utf-8 -*-

# Created by Hardy on 07th, Aug, 2017
# Copyright 2017 杭州网川教育有限公司. All rights reserved.

import heapq
import json

from querier.esquerier import ElasticSearchQuerier
import utils.utils as utils
from utils.doc_parser import doc_parser


class ParaCombineQuerier(ElasticSearchQuerier):

    def __init__(self, para_search, img_search=None, nlp_service=None):
        super(ParaCombineQuerier, self).__init__(None, None, None)
        self.nlp_service = nlp_service
        self.para_search = para_search
        self.img_search = img_search

    def _build_query(self, args): pass

    def search(self, args):
        url = args.get('url')
        if url is None:
            raise ValueError('message: "url"("wechat page url") is needed')

        content = doc_parser(url)
        out = []
        for c in content:
            if c.get('type') == 'text':
                text = c.get('content')
                search_args = {
                    'term': text,
                    'from': 0,
                    'size': 5
                }
                result = self.para_search.search(search_args)
                out.append(result.get('articles', []))

        return {"articles": out}

    def _build_result(self, es_result, param): pass
